<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表格Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
</head>

<body>
    <form class="layui-form user-from" lay-filter="example">

        <input type="hidden" name="regionId" class="layui-input">

        <div class="layui-form-item">
            <label class="layui-form-label">澡堂名称</label>
            <div class="layui-input-block">
                <input type="text" name="regionName" placeholder="请输入" required autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">澡堂单价</label>
            <div class="layui-input-block">
                <select name="regionPrice">
                    <option value="10">10/h</option>
                    <option value="20">20/h</option>
                    <option value="30">30/h</option>
                    <option value="40">40/h</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">创建时间</label>
            <div class="layui-input-block">
                <input name="regionDate" id="regionDate" required lay-verify="required" placeholder="请输入"
                    class="layui-input"></input>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">澡堂状态</label>
            <div class="layui-input-block">
                <select name="regionState">
                    <option value="空闲">空闲</option>
                    <option value="使用中">使用中</option>
                    <option value="已预约">已预约</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">澡堂简介</label>
            <div class="layui-input-block">
                <textarea name="regionIntroduce" lay-verify="required" placeholder="请输入"
                    class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="uploadBtn">澡堂图片</button>
            <div class="layui-upload-list" id="previewBox">

            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 40px;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>


    </form>
    <script>

        layui.use(['upload', 'form', 'jquery', 'laydate'], function () {
            var form = layui.form;
            var $ = layui.jquery;
            var upload = layui.upload;
            var laydate = layui.laydate;
            var files = [];
            var srcArr = new Array();

            laydate.render({
                elem: '#regionDate',
                value: new Date()
                , type: 'datetime'
                , trigger: 'click'
            });

            upload.render({
                elem: '#uploadBtn'
                , multiple: true
                , auto: false
                , choose: (obj) => {//预览图片
                    files = obj.pushFile();
                    obj.preview((index, file, result) => {
                        $('#previewBox').append('<div class="imgBox" ><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>')
                        $('.remove_' + index).bind('click', function () {
                            delete files[index];//删除指定图片
                            $(this).parent().remove();
                        })
                    });
                }
            });

            var req = GetRequest();
            var regionId = req['regionId'];
            $.ajax({
                url: '/getRegionById',
                data: {
                    regionId: regionId
                },
                dataType: 'json',
                type: 'Post',
                success: function (res) {
                    data = res.data;
                    form.val("example", data);
                    srcArr = data.regionImgs;

                    srcArr.forEach((item, index) => {
                        $('#previewBox').append('<div class="imgBox" ><img src="' + item + '" alt="" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>');
                        $('.remove_' + index).bind('click', function () {
                            let itemIndex = $(this).parent().parent().find('.imgBox').index($(this).parent()[0]);
                            srcArr.splice(itemIndex, 1);
                            $(this).parent().remove();
                        })
                    })
                }
            })

            $('#submit').on('click', function () {
                var data = form.val('example');
                var { regionName, regionPrice, regionIntroduce, regionDate, regionId, regionState } = data
                var formData = new FormData();
                for (var i = 0; i < Object.values(files).length; i++) {
                    formData.append('regionImg', Object.values(files)[i]);
                }
                formData.append('regionName', regionName);
                formData.append('regionId', regionId);
                formData.append('regionPrice', regionPrice);
                formData.append('regionState', regionState);
                formData.append('regionIntroduce', regionIntroduce);
                formData.append('regionDate', regionDate);
                formData.append("srcArr", JSON.stringify(srcArr));
                $.ajax({
                    type: "POST",
                    url: "/updateRegion",
                    async: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            alert("修改成功");
                            xadmin.close();
                            xadmin.father_reload();
                        } else {
                            alert(data.msg);
                        }
                    },
                    error() {
                        alert("文件太大")
                    }
                });
                return false;
            });
        })
    </script>


</body>